const ArticleModel = require('../modules/koa2-demo')

class articleController {
  /**
     * 创建文章
     * @param ctx
     * @returns {Promise.<void>}
     */
  static async create(ctx) {
    // 接收客服端
    const { request } = ctx;
    const req_query = request.query;
    console.log('req_query', req_query.name, req_query.content)
    if (
      req_query.name // 名称
            && req_query.content // 内容
    ) {
      try {
        // 创建文章模型
        const ret = await ArticleModel.createDemo(req_query);
        // const data = await ArticleModel.getDemo(ret.name);
        ctx.response.status = 200;
        ctx.body = {
          code: 200,
          msg: '创建文章成功',
          data: '123',
        }
      } catch (err) {
        console.log(err)
        ctx.response.status = 412;
        ctx.body = {
          code: 200,
          msg: '创建文章失败',
          data: err,
        }
      }
    } else {
      ctx.response.status = 416;
      ctx.body = {
        code: 200,
        msg: '参数不齐全',
      }
    }
  }

  static async detail(ctx) {
    console.log('ctx', ctx.query)
    const { name } = ctx.query;
    console.log(name, 'name')
    if (name) {
      try {
        // 查询文章详情模型
        const data = await ArticleModel.getDemo(name);
        console.log('data', data)
        ctx.response.status = 200;
        ctx.body = {
          result: '000000',
          msg: '查询成功',
          data,
        }
      } catch (err) {
        ctx.response.status = 412;
        ctx.body = {
          result: '111111',
          msg: '查询失败',
          data: err,
        }
      }
    } else {
      ctx.response.status = 416;
      ctx.body = {
        result: '111111',
        msg: 'name必须传',
      }
    }
  }
}

module.exports = articleController
